package duaxe.home;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;

import javax.swing.JFrame;

import duaxe.home.Graphics.Screen;
import duaxe.home.Graphics.Sprite;
import duaxe.home.entity.mob.Player;
import duaxe.home.input.Keyboard;
import duaxe.home.input.Mouse;
import duaxe.home.level.Level;
import duaxe.home.level.RandomLevel;
import duaxe.home.level.SpawnLevel;
import duaxe.home.level.TileCoordinate;

public class Game extends Canvas implements Runnable {
	
	private static final long serialVersionUID = 1L;
	public static int width =300;
	public static int height = 168;
	public static int scale =3;
	public String title ="home";
	
	private Thread thread;
	private JFrame frame;
	private Keyboard key;
	private Level level;
	private Player player;
	private boolean running = false;
	
	private Screen screen;
	
	
	private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	private int[] pixels =((DataBufferInt)image.getRaster().getDataBuffer()).getData();
	
	public Game(){
		Dimension size = new Dimension(width*scale, height*scale);
		setPreferredSize(size);
		
		screen = new Screen(width, height);
		frame = new JFrame();
		key = new Keyboard();
		//level = new RandomLevel(64,64);
//		level = new SpawnLevel("/textures/levels/level.png");
		level = Level.spawn;
		TileCoordinate player_spawn = new TileCoordinate(10, 16);
		player = new Player(player_spawn.x(),player_spawn.y(), key);
		player.init(level);
		addKeyListener(key);

		Mouse mouse = new Mouse();
		addMouseListener(mouse);
		addMouseMotionListener(mouse);
		
	}
	public static int getWindowWidth(){
		return width*scale;
	}
	public static int getWindowHeight(){
		return height*scale;
	}
	
	
	public synchronized void start(){
		running = true;
		thread = new Thread(this, "Display");
		thread.start();
	}
	
	public synchronized void stop(){
		running = false;
		try{
			
			thread.join();
			
		}catch(InterruptedException ex){
			ex.printStackTrace();
		}
	}

	public void run(){
		long lastTime = System.nanoTime();
		long timer = System.currentTimeMillis();
		final double ns = 1000000000.0/60.0;
		double delta =0;
		int frames =0;
		int updates =0;
		requestFocus();

		while(running){
			long now = System.nanoTime();
			delta+=(now-lastTime)/ns;
			
			lastTime = now;
			while(delta>=1){
				update();
				updates++;
				delta--;
			}
			render();
			frames++;
			
			if(System.currentTimeMillis() - timer>1000){
				timer+=1000;
				//System.out.println(updates +" ups, "+ frames+ " frs");
				frame.setTitle(title+" | "+updates +" ups "+ frames+ " frs" );
				updates =0;
				frames =0;
			}
		}
		stop();
	}
//	int x=0;
//	int y=0;
	public void update(){
		key.update();
		player.update();
		level.update();
//		if(key.up) y--;
//		if(key.down) y++;
//		if(key.left) x --;
//		if(key.right) x++;
		
		
	}
	public void render(){
		BufferStrategy bs = getBufferStrategy();
		if(bs==null){
			createBufferStrategy(3);
			return;
		}
		screen.clear();
		int xScroll = player.x - screen.width/2;
		int yScroll = player.y - screen.height/2;
		
		level.render(xScroll, yScroll, screen);
		player.render(screen);
		
//		Random random = new Random();
//		Sprite sprite = new Sprite(2, 1, 0xffffff);
//		for(int i=0; i<100;  i++){
//			int x = random.nextInt(100);
//			int y = random.nextInt(100);
//		screen.renderSprite(width -60+x,50-y, sprite, true);
//		}
//		
		for(int i=0; i<pixels.length; i++){
			pixels[i] = screen.pixels[i];
		}
		Graphics g = bs.getDrawGraphics();
		g.drawImage(image, 0, 0, getWidth(),getHeight(), null);
		
		
		g.setColor(Color.WHITE);
		g.setFont(new Font("Courier New", 0, 50));
		//g.drawString("x: "+player.x+ "  y: "+ player.y, 350, 300);
		//g.fillRect(Mouse.getX()-30, Mouse.getY()-32,64, 64);
		//if(Mouse.getButton() != -1)g.drawString("Mouse: "+ Mouse.getButton(), 80,80);
		
		g.dispose();
		bs.show();
		
	}
	
	public static void main(String[] args) {
		Game game = new Game();
		game.frame.setResizable(false);
		game.frame.setTitle(game.title);
		game.frame.add(game);
		game.frame.pack();
		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game.frame.setLocationRelativeTo(null);
		game.frame.setVisible(true);
		
		game.start();
		
	}
}
